package com.jhws.business.comm.service;

import com.jhws.business.comm.bo.RoomType;
import com.jhws.core.db.BaseDao;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RoomTypeService {
    @Resource
    private BaseDao BaseDao;

    /**
     * 保存户型信息
     *
     * @param roomType 户型信息
     */
    public void saveRoomType(RoomType roomType) {
        if (roomType.getCreationTime() == null) {
            roomType.setCreationTime(new Date());
        }
        BaseDao.saveOrUpdate(roomType);
    }

    /**
     * 查询户型信息
     *
     * @param id 户型信息ID
     */
    public RoomType queryRoomType(long id) {
        return BaseDao.get(RoomType.class, id);
    }

    /**
     * 条件查询户型信息列表
     *
     * @param where 条件
     */
    public List<RoomType> queryRoomTypes(String where) {
        return BaseDao.findList("from RoomType " + where);
    }

    /**
     * 分页查询户型信息列表
     *
     * @param param 条件
     */
    public Map queryRoomTypePage(Map param) {
        String hql = "from RoomType where 1=1 ";
        String roomTypeName = MapUtils.getString(param, "roomTypeName");
        String commCode = MapUtils.getString(param, "commCode");
        if (StringUtils.isNotEmpty(commCode)) {
            hql += "and comm_code= '" + commCode + "' ";
        }
        if (StringUtils.isNotEmpty(roomTypeName)) {
            hql += "and room_type_name like '%" + roomTypeName + "%' ";
        }
        return BaseDao.queryPage(hql);
    }

    /**
     * 删除户型信息
     *
     * @param roomType 户型信息ID
     */
    public void deleteRoomType(RoomType roomType) {
        BaseDao.delete(roomType);
    }

    /**
     * 条件删除户型信息
     *
     * @param where 条件
     */
    public void deleteRoomType(String where) throws Exception {
        String sql = "delete from room_type " + where;
        BaseDao.executeBySql(sql);
    }
}
